<?php

App::uses('AppController', 'Controller');
App::uses('AttachmentBehavior', 'Uploader.Model/Behavior');

class SiteController extends AppController {
	
	public function beforeFilter() {
		
		parent::beforeFilter();
		
		// check launch mode
		if( Configure::read('App.launching') ) {
			if( $this->name != 'Site' || $this->action != 'launch' ) {
				$this->redirect( array('controller' => 'site', 'action' => 'launch') );
			}
		}
		
		// check maintenance mode
		if( Configure::read('App.maintenance') ) {
			if( $this->name != 'Site' || $this->action != 'maintenance' ) {
				$this->redirect( array('controller' => 'site', 'action' => 'maintenance') );
			}
		}
		
	}
	
	public function home() {
		
	}
	
	public function about() {
		
	}
	
	public function launch() {
		
		// diff
		$launchTime = strtotime( $this->appSettings['App']['launchTime'] );
		$now = strtotime( date('Y-m-d H:i:s') );
		$diff = $launchTime - $now;
		
		// months / weeks / days
		$months = floor($diff / (30 * 24 * 3600));
		$weeks  = floor($diff / (7 * 24 * 3600));
		$days   = floor($diff / (24 * 3600));
		
		if( $months > 0 ) {
			$label = $months > 1 ? __('months') : __('month');
			$this->set('remainingTime', __('in %d %s', $months, $label));
		}
		else if( $weeks > 0 ) {
			$label = $weeks > 1 ? __('weeks') : __('week');
			$this->set('remainingTime', __('in %d %s', $weeks, $label));
		}
		else if( $days > 0 ) {
			$label = $days > 1 ? __('days') : __('day');
			$this->set('remainingTime', __('in %d %s', $days, $label));
		}
		else {
			$this->set('remainingTime', __('SOON'));
		}
		
		// time to launch
		$timeToLaunch = sprintf('%02d:%02d:%02d:%02d', $days, date('H', $diff), date('i', $diff), date('s', $diff));
		$this->set('timeToLaunch', $timeToLaunch);
	}
	
	public function maintenance() {
		
	}
	
}